/*
 * @Author: lirl
 * @Date: 2020-11-25 10:15:34
 * @LastEditTime: 2024-08-02 10:55:47
 * @Description: 列表公共混入文件
 */
import { PAGE_INITIAL_OFFSET } from '@/utils/constants';
import { omit } from 'lodash-es';
import dayjs from 'dayjs';
import { alertErrorMsg } from '@/api/utils/pc-message';
const { SIZE_OFFSET, PAGE_OFFSET } = PAGE_INITIAL_OFFSET
export default {
  data() {
    return {
      // 数据请求service
      service: null,
      // 列表数据
      tableData: [],
      // 获取数据的方法
      functionName: 'getList',
      // 自定义查询参数
      searchParams: {
        range: [],
      },
      // 表格数据加载状态
      loading: false,
      // 总条数
      total: 0,
      // 每页条数
      pageSize: 10,
      // 当前页码
      pageNumber: 1,
      // 表格参数
      tableAttrs: {
        'header-cell-class-name': 'header-cell-class',
        'highlight-current-row': false
      },
      // 分页事件处理方法集合
      paginationEvents: {
        'current-change': this.getList,
        'size-change': this.sizeChange
      },
      // 表格事件集合
      tableEvents: {
        'row-click': this.openTaskForm,
      }
    };
  },
  computed: {
    /**
     * 计算每一页开始索引
     */
    startIndex() {
      return (this.pageNumber - PAGE_OFFSET) * this.pageSize + SIZE_OFFSET;
    },

    /**
     * 获取数据请求参数，把查询参数和分页数据合并为请求后台参数
     */
    param() {
      return {
        ...omit(this.searchParams, ['range']),
        page: this.pageNumber,
        pageSize: this.pageSize
      };
    },
    /**
     * 分页组件参数
     */
    paginationAttrs() {
      return {
        total: this.total,
        layout: 'prev, pager, next, sizes, jumper, total',
        pagerCount: 9,
        background: true,
        'current-page': this.pageNumber,
        'page-size': this.pageSize,
        'page-sizes': [10, 20, 50, 100, 200]
      };
    },
  },
  watch: {
    range(val) {
      if (val) {
        const { searchParams } = this;
        this.$set(searchParams, 'startTime', val[0]);
        this.$set(searchParams, 'endTime', dayjs(val[1]).add(1, 'day').valueOf() - 1);
      }
    }
  },
  methods: {
    /**
     * 根据查询参数获取表格数据
     * @param {Number} pageNum 当前查询的页码，默认1
     * @param {Boolean} isDelete 是否是删除后回刷列表
     */
    async getList(cur = 1) {
      this.pageNumber = cur;
      try {
        this.loading = true;
        const data = await this.service[this.functionName]({
          ...this.param,
          ...(this.otherParams ?? {}),
        });
        this.total = data.total;
        this.tableData = data.pageList ?? data.records;
      } catch (error) {
        alertErrorMsg(error);
      } finally {
        this.loading = false;
      }
    },
    /**
     * pageSize 改变时会触发
     * @param {Number} size 每页条数
     */
    sizeChange(size) {
      this.pageSize = size;
      this.getList();
    },
    /**
     * 点击重置按钮
     */
    resetFields() {
      this.pageNumber = 1;
      this.searchParams = {
        range: [],
      };
      this.getList();
    },
    /**
     * 点击搜索按钮
     */
    search() {
      this.page = 1;
      this.getList();
    },
    getTableRowIndexNo(index) {
      return (this.pageNumber - 1) * this.pageSize + index + 1;
    }
  }
};
